<?php

/**
 * @package SCS
 * @subpackage Category
 * @author Igor.Barkowsky@gmail.com
 * @copyright 2008
 */

class CategoryWizard
{
	var $table;
	var $db;
	var $smarty;
	var $iname='id';
	var $pname='pid';
	var $padding=3;
	var $padding_element='&nbsp;';
	function __construct($tablename='category')
	{
		global $db,$smarty;
		$this->db = $db;
		$this->smarty = $smarty;
		$this->table = $this->db->pfx.mysql_real_escape_string($tablename);
	}
	function html_options($parent = 0, $selected = 0, $status=2, $level_limiter = 0, $level=0)
	{
		static $selected,$level_limiter;
		$selected = intval($selected);
		$level_limiter = intval($level_limiter);
		$status = intval($status);
		$parent = intval($parent);
		$level = intval($level);
		$str = '';
		$query = "
		SELECT 
		$this->iname,$this->pname,title 
		FROM $this->table 
		WHERE $this->pname = '$parent' AND $this->iname<>'0' AND status>='$status' 
		ORDER BY order_num";
		$sql = $this->db->fetch_array($query,MYSQL_ASSOC);
		if ($sql!==false)
		{
			foreach($sql as $k=>$row)
			{	
				$str .= '<option value="'.$row[$this->iname].'"'.((intval($selected)==$row[$this->iname])?' selected':'').'>'.(($level>0)?str_repeat($this->padding_element, $level*$this->padding):'').$row['title'].'</option>';
	    		$next_level = $level + 1;
	    		if($level_limiter==0 or $level_limiter>=$next_level)
					$str .= $this->html_options($row[$this->iname],$selected,$status,$level_limiter,$next_level);
			}
		}
		return $str;
	}
	function recurse($parent = 0, $selected = 0, $status = 0, $level_limiter = 0,$level = 0)
	{
		$level_limiter = intval($level_limiter);
		$status = intval($status);
		$parent = intval($parent);
		$pages = array();
		$inside_selected = false;
		/** Query for category non-top level **/
		$query = "
		SELECT 
			par.$this->iname,par.$this->pname,par.title,par.alias,COUNT(DISTINCT ch.id) AS childs 
		FROM $this->table AS par 
		LEFT JOIN $this->table AS ch ON (par.$this->iname = ch.$this->pname AND ch.status >='$status') 
		WHERE 
			par.$this->pname = '$parent' AND par.$this->iname<>'0' AND par.status>='$status' 
		GROUP BY par.id 
		ORDER BY par.order_num";
		$rows = $this->db->fetch_array($query);
		//print_var($rows);
		$i = 0;
		foreach ($rows as $k=>$row)
		{
			$pages[$i][$this->iname] = $row[$this->iname];
			if (is_array($selected))
			{
	            if (in_array($row[$this->iname], $selected))
	    		{
	    			$pages[$i]['selected'] = 'selected';
	    			$inside_selected = true;
	    		}
	        }
	        else
	        {
	    		if (intval($selected) == $row[$this->iname])
	    		{
	    			$pages[$i]['selected'] = 'selected';
	    			$inside_selected = true;
	    		}
	    	}
			$pages[$i]['title'] = $row['title'];
			$pages[$i]['level'] = $level;
			$pages[$i]['alias'] = $row['alias'];
			$pages[$i]['childs']=intval($row['childs']);
	
			$next_level = $level + 1;
			if($level_limiter == 0 or $next_level <= $level_limiter)
			{
				if ($pages[$i]['childs']>0)
				{
		            $sub = $this->recurse($row[$this->iname], $selected, $display,$level_limiter,$next_level);
					$pages[$i]['sub']=$sub['pages'];
					if($sub['inside_selected'] == true)
					{
						$pages[$i]['inside_selected'] = true;
						$inside_selected = true;
					}
				}
			}
			$i++;
		}
		return array('pages'=>$pages,'inside_selected'=>$inside_selected);
	}
	function get_category_info($cat_id = 0)
	{
		if(isset($cat_id) and intval($cat_id) != 0)
		{
			$info = array();
			$info = $this->db->fetch_one("SELECT * FROM $this->table WHERE $this->iname = '".intval($cat_id)."'");
			if($info != false)
			{
				return $info;
			}
			else return -1;
		}
		else return false;
	}
}

?>